using System;
using System.Collections.Generic;
using System.Drawing;
using pub.CmdUI.UI.Control;

namespace pub.CmdUI.Cmd
{
    using pub.NET20Fix;
    
    public class DefaultCmd : ICmd, ICmdState
    {
        public Action<ICmd> OnExecute;
        public Func<ICmd, bool> OnGetEnabled { get; set; }
        public Func<ICmd, bool?> OnGetChecked { get; set; }
        private string _name;

        public DefaultCmd()
        {
            Shortcuts = new List<CmdShortcut>();
            _name = this.GetType().Name;
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Description { get; set; }

        public CmdCategory Category { get; set; }

        public ICollection<CmdShortcut> Shortcuts { get; set; }

        public CmdShortcut Shortcut
        {
            set
            {
                Shortcuts.Clear();
                Shortcuts.Add(value);
            }
        }

        public ICmdState State
        {
            get { return this; }
        }

        public virtual void Execute(IControlUI controlUi)
        {
            if (OnExecute != null)
            {
                OnExecute(this);
            }
        }

        public Bitmap Icon
        {
            get { return null; }
        }

        public string Label
        {
            get { return this.Name; }
        }

        public string Tooltip
        {
            get { return this.Description; }
        }

        public bool Enabled
        {
            get
            {
                if (OnGetEnabled != null)
                {
                    return OnGetEnabled(this);
                }
                return true;
            }
        }

        public bool? Checked
        {
            get
            {
                if (OnGetChecked != null)
                {
                    return OnGetChecked(this);
                }
                return false;
            }
        }

        public bool Visible
        {
            get { return true; }
        }

        public int? SelectedIndex
        {
            get { return null; }
        }
    }
}